//
//  WebView.m
//  云成绩
//
//  Created by Apple1 on 2017/3/10.
//  Copyright © 2017年 zj. All rights reserved.
//

#import "WebContainer.h"




#import <WebKit/WebKit.h>
@implementation WebContainer {
    __weak WKWebView *_web;
}

- (void)setNavigationDelegate:(id<WKNavigationDelegate>)navigationDelegate {
    _web.navigationDelegate = navigationDelegate;
}

- (void)setUIDelegate:(id<WKUIDelegate>)UIDelegate {
    _web.UIDelegate = UIDelegate;
}

//- (void)setScrollView:(UIScrollView *)scrollView{
//    _web.scrollView = scrollView;
//}

- (void)loadRequest:(NSURLRequest *)request {
    [_web loadRequest:request];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKWebView *web = [[WKWebView alloc] initWithFrame:self.bounds configuration:config];
    web.tag = self.tag + 100;
    web.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:web];
    _web = web;
}

- (void)evaluateJavaScript:(NSString * _Nullable)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler {
    [_web evaluateJavaScript:javaScriptString completionHandler:completionHandler];
}

- (void)loadHTMLString:(nonnull NSString *)string baseURL:(nullable NSURL *)baseURL{
    [_web loadHTMLString:[self reSizeImageWithHTML:string] baseURL:baseURL];
}

- (NSString *)reSizeImageWithHTML:(NSString *)html {
    return [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%fpx}</style>%@", kWIDTH - 20, html];
}

//- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(id _Nullable di, NSError * _Nullable error))completionHandle{
//    [_web evaluateJavaScript:javaScriptString completionHandler:^(id _Nullable di, NSError * _Nullable error) {
//        
//    }];
//}

@end
